package com.deyuanyun.pic.ctrl.util;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.tool.Globals;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.ctrl.annotation.*;
import com.deyuanyun.pic.ctrl.domain.EqptCptestStation;
import com.deyuanyun.pic.ctrl.excelImport.InterferenceMonitoringExcel;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.ctrl.service.ImportService;
import com.deyuanyun.pic.common.webservice.UtilsServiceClient;
import com.deyuanyun.pic.pipe.domain.CenterLine;
import com.deyuanyun.pic.pipe.service.PipeCenterLineService;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.print.DocFlavor;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

/**
 * 倒入校验
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/14）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class AutoImport {

    /**
     * 检查是否存在计算绝对距离的必须条件
     * pipeLineId，positionX，positionY
     * @param cla
     * @return
     * @throws NoSuchFieldException
     */
    private static boolean checkFiledXYZ(Class cla) throws NoSuchFieldException {
        try {
            Field pipeLineId = cla.getDeclaredField("pipeLineId");
            Field positionX = cla.getDeclaredField("positionX");
            Field positionY = cla.getDeclaredField("positionY");

            return pipeLineId != null && positionX != null && positionY != null;

        }catch(Exception e){
            return false;
        }
    }


    private static boolean checkFiledXYZValue(Object obj) throws NoSuchFieldException {
        try {
            String pipeLineId = BeanUtils.getSimpleProperty(obj,"pipeLineId");
            String positionX = BeanUtils.getSimpleProperty(obj,"positionX");
            String positionY = BeanUtils.getSimpleProperty(obj,"positionY");

            return !StringUtils.isEmpty(pipeLineId) &&!StringUtils.isEmpty(positionX) &&!StringUtils.isEmpty(positionY);

        }catch(Exception e){
            return false;
        }
    }

    /**
     * 判断是否包含测试桩查询绝对距离
     * @param cla
     * @return
     * @throws NoSuchFieldException
     */
    private static boolean checkFiledNagm(Class cla) throws NoSuchFieldException {
        try {
            Field nagm = cla.getDeclaredField("nagm");
            Field nagmMileage = cla.getDeclaredField("nagmMileage");
            return nagm != null && nagmMileage != null ;

        }catch(Exception e){
            return false;
        }
    }

    private static boolean checkFiledNagmVal(Object obj) throws NoSuchFieldException {
        try {
            String nagm = BeanUtils.getSimpleProperty(obj,"nagm");
            String nagmMileage = BeanUtils.getSimpleProperty(obj,"nagmMileage");

            return !StringUtils.isEmpty(nagm)&& !StringUtils.isEmpty(nagmMileage) ;

        }catch(Exception e){
            return false;
        }
    }


    private static boolean checkFiledTestStationId(Class cla) throws NoSuchFieldException {
        try {
            Field nagm = cla.getDeclaredField("testStationId");
            Field nagmMileage = cla.getDeclaredField("relativeTestPileDistance");
            return nagm != null && nagmMileage != null ;

        }catch(Exception e){
            return false;
        }
    }



    private static boolean checkFiledTestStationIdVal(Object obj) throws NoSuchFieldException {
        try {
            String nagm = BeanUtils.getSimpleProperty(obj,"testStationId");
            String nagmMileage = BeanUtils.getSimpleProperty(obj,"relativeTestPileDistance");

            return !StringUtils.isEmpty(nagm)&& !StringUtils.isEmpty(nagmMileage) ;

        }catch(Exception e){
            return false;
        }
    }

    private static  boolean checkFiled(Object obj){

        Field oclpointId = null;
        try{
            //有漏损点，就使用漏损点绝对距离
            oclpointId = obj.getClass().getDeclaredField("oclpointId");
            String oclpointIdVal = BeanUtils.getSimpleProperty(obj,"oclpointId");
            if(!StringUtils.isEmpty(oclpointIdVal))
            {
                return  true;
            }
        }catch(Exception e2)
        {

        }
        return  false;
    }
    /**
     * 初始化附近测试桩
     * @param data
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void initNagm(List<? extends AutoImportInterface> data) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        if(data.size() > 0)
        {
            ImportService importService= SpringAppliction.getBean(ImportService.class);
            Class cla = data.get(0).getClass();
            try{
                Field field =  null;
                try{
                    field = cla.getDeclaredField("nagm");
                }catch (Exception e2){}
                //不是用户填写的
                if(field.getAnnotation(Head.class) == null)
                {
                    Method getPipeLineId=cla.getMethod("getPipeLineId");
                    Method getMileage=cla.getMethod("getMileage");
                    Method setNagm=cla.getMethod("setNagm",String.class);
                    Method setNagmMileage=cla.getMethod("setNagmMileage",String.class);
                    if(getPipeLineId != null && getMileage != null && setNagm != null && setNagmMileage != null)
                    {
                        String mileage=null;
                        String pipeLineId=null;
                        HashMap nagm = null;
                        BigDecimal nagmMileage=null;
                        for(int i=0,end=data.size();i < end;i++)
                        {
                            mileage = (String) getMileage.invoke(data.get(i));
                            pipeLineId = (String) getPipeLineId.invoke(data.get(i));
                            if(!StringUtils.isEmpty(mileage) && !"null".equals(mileage))
                            {
                                nagm=importService.queryNagm(pipeLineId,mileage);
                                nagmMileage= (BigDecimal) nagm.get("MILEAGE");

                                BeanUtils.setProperty(data.get(i),"nagm",nagm.get("ID"));
                                BeanUtils.setProperty(data.get(i),"nagmMileage",""+nagmMileage.longValue());
                            }
                        }
                    }
                }


            }catch(Exception e){
                    e.printStackTrace();
            }



        }

    }

    /**
     * 获取对象的绝对距离 条件
     * @param data
     * @return
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public static String getPointData(List<Object> data) throws Exception {
        List process = new ArrayList();
        ImportService importService = SpringAppliction.getBean(ImportService.class);
        EQPTBaseService service= SpringAppliction.getBean(EQPTBaseService.class);
        //将存在xyz的对象取出来
        for(Object obj:data)
        {
            Class clas = obj.getClass();
            if(checkFiledXYZ(clas) && checkFiledXYZValue(obj))
            {
                process.add(obj);
            }else{
                //通过最近测试桩和相对测试桩距离计算绝对距离
                if(checkFiledNagm(clas) && checkFiledNagmVal(obj))
                {
                    String testId = BeanUtils.getSimpleProperty(obj,"nagm");
                    String nagmMileage = BeanUtils.getSimpleProperty(obj,"nagmMileage");

                    EqptCptestStation  test =service.queryObjectByPrimaryKey(EqptCptestStation.class,testId);
                    if(test.getMileage() != null && !StringUtils.isEmpty(nagmMileage))
                    {
                        Double mileage = test.getMileage().doubleValue() +   Double.parseDouble(nagmMileage);
                        BeanUtils.setProperty(obj,"mileage",""+mileage.longValue());
                    }

                }else if(checkFiledTestStationId(clas) && checkFiledTestStationIdVal(obj))
                {
                    String testId = BeanUtils.getSimpleProperty(obj,"testStationId");
                    String nagmMileage = BeanUtils.getSimpleProperty(obj,"relativeTestPileDistance");
                    EqptCptestStation  test =service.queryObjectByPrimaryKey(EqptCptestStation.class,testId);
                    if(test.getMileage() != null && !StringUtils.isEmpty(nagmMileage))
                    {
                        Double mileage = test.getMileage().doubleValue() +   Double.parseDouble(nagmMileage);
                        BeanUtils.setProperty(obj,"mileage",""+mileage.longValue());
                    }
                }
            }
        }

        String fs = Globals.getProperties("mileageMethod");
        if(fs == null)
        {
            String result=getPointData(process,new HandlerUtil());
            if(result != null)
            {
                try{
                    List<Double> mileageList=UtilsServiceClient.getMileage(result);
                    getPointData(process,new HandlerMileage(mileageList));
                }catch (Exception e)
                {
                    throw new Exception("通过webService查询绝对距离出错,错误信息:"+e.getMessage());
                }



            }
            return result;
        }

      return "";
    }

    /**
     * 获取
     * @param data
     * @return
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    private static String getPointData(List<Object> data,HandlerService handler) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        PipeCenterLineService pipeCenterLineService =SpringAppliction.getBean(PipeCenterLineService.class);

        if(data.size() > 0)
        {
            Class clas=data.get(0).getClass();
            if(checkFiledXYZ(clas))
            {
                for(int i=0,end=data.size();i < end; i++)
                {
                    Method getPipeLineId=clas.getMethod("getPipeLineId");
                    Method getPositionX=clas.getMethod("getPositionX");
                    Method getPositionY=clas.getMethod("getPositionY");

                    String pipeLineId= (String) getPipeLineId.invoke(data.get(i));
                    String positionX= (String) getPositionX.invoke(data.get(i));
                    String positionY= (String) getPositionY.invoke(data.get(i));
                    CenterLine centerLine  =pipeCenterLineService.getCenterLineByLineId(pipeLineId);
                    if(centerLine != null)
                    {
                        pipeLineId = centerLine.getGisId();
                    }


                    if(!StringUtils.isEmpty(pipeLineId)&&
                            !StringUtils.isEmpty(positionX)&&
                            !StringUtils.isEmpty(positionY)
                            )
                    {
                        handler.callback(data.get(i),pipeLineId,positionX,positionY);
                    }
                }


                return handler.getExecuteResult();
            }else{
                return null;
            }
        }
        return  null;
    }

    /**
     * 读取交直流24小时干扰数据
     * @param in
     * @param startIndex
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public static List<InterferenceMonitoringExcel> readExcelToObject(InputStream in,int startIndex) throws IOException, ParseException {

        List<InterferenceMonitoringExcel> result=new ArrayList<InterferenceMonitoringExcel>();

        Workbook wb  = null;
        if (!in.markSupported()) {
            in = new PushbackInputStream(in, 8);
        }
        if(POIXMLDocument.hasOOXMLHeader(in))
        {
            wb = new XSSFWorkbook(in);
        }
        else if(POIFSFileSystem.hasPOIFSHeader(in))
        {
            wb = new HSSFWorkbook(in);
        }
        Sheet sheet = wb.getSheetAt(0);

        InterferenceMonitoringExcel interferenceMonitoringExcel = null;
        for(int i=startIndex,end=sheet.getLastRowNum();i < end;i++)
        {
            Row row = sheet.getRow(i);
            interferenceMonitoringExcel = new InterferenceMonitoringExcel();
            interferenceMonitoringExcel.setTestTime(getCellValString(row.getCell(0)));
            interferenceMonitoringExcel.setAcInterferenceVoltage(getCellValString(row.getCell(1)));
            result.add(interferenceMonitoringExcel);
        }


        return result;
    }

    public static String getCellValString(Cell cell) throws ParseException {
        String cellStr="";
        if (cell == null) {
            cellStr = "";
        }else {
            if (cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)){
                    Date date = cell.getDateCellValue();
                    cellStr= DateUtil.DateTimeToString(date);
                }else {
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cellStr = cell.toString() + "";
                }
            }else if (cell.getCellType()==XSSFCell.CELL_TYPE_FORMULA){
                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                cellStr = cell.getStringCellValue();
            }else if (cell.getCellType()==XSSFCell.CELL_TYPE_STRING){
                cellStr = cell.getStringCellValue();
            }else {
                cellStr=cell.toString();
            }
        }

        if (cellStr!=null){
            cellStr=cellStr.trim();
        }
        return cellStr;
    }


    /**
     * 检查导入文件头
     * @param in
     * @param cla
     * @return
     * @throws IOException
     */
    public static String checkHead(InputStream in,Class cla) throws Exception {

        StringBuilder result = new StringBuilder();

        Workbook wb  = null;
        if (!in.markSupported()) {
            in = new PushbackInputStream(in, 8);
        }
        if(POIXMLDocument.hasOOXMLHeader(in))
        {
            wb = new XSSFWorkbook(in);
        }
        else if(POIFSFileSystem.hasPOIFSHeader(in))
        {
            wb = new HSSFWorkbook(in);
        }
        Sheet sheet = wb.getSheetAt(0);
        Field fields[] = cla.getDeclaredFields();
        Head head=null;
        String headName=null;
        Cell cell=null;
        for(int i=0,end=fields.length;i < end;i++)
        {
            head=fields[i].getAnnotation(Head.class);
            if(head != null )
            {
                if(sheet.getRow(head.row()) ==null)
                {
                    result.append("索引号为:").append("第").append(head.row()+1).append("行").append(head.cell()+1).append("列>>").append("&nbsp;&nbsp;计划名称:[").append(head.name()).append("]&nbsp;&nbsp;实际名称:").append("<br/>");
                    continue;
                }
                cell = sheet.getRow(head.row()).getCell(head.cell());
                if(cell != null)
                {

                    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
                    {
                        headName = ""+cell.getNumericCellValue();
                    }else{
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        headName = cell.getStringCellValue();
                    }


                    headName=headName.replaceAll("\r","").replaceAll("\n","");
                    if(!headName.equals(head.name()))
                    {
                        if(result.length() == 0)
                        {
                            result.append("<span style='color:#ff7575'>导入模板错误</span><br/>");
                        }

                        result.append("索引号为:").append("第").append(head.row()+1).append("行").append(head.cell()+1).append("列>>").append("&nbsp;&nbsp;计划名称:[").append(head.name()).append("]&nbsp;&nbsp;实际名称:[").append(headName).append("]<br/>");
                    }
                }else{
                    if(result.length() == 0)
                    {
                        result.append("<span style='color:#ff7575'>导入模板错误</span><br/>");
                    }
                    result.append("索引号为:").append("第").append(head.row()+1).append("行").append(head.cell()+1).append("列>>").append("&nbsp;&nbsp;计划名称:[").append(head.name()).append("]&nbsp;&nbsp;实际名称:").append("<br/>");

                }

            }
        }
        wb.close();
        return result.toString();
    }

    /**
     * 获取关联类
     * @param cla
     * @return
     */
    private static List<Field> getMappingField(Class cla)
    {
        List<Field> result=new ArrayList<Field>();
        MappingTable mappTabl=null;

        Field fields[] = cla.getDeclaredFields();
        for(int i=0,end=fields.length;i<end;i++)
        {
            mappTabl = fields[i].getAnnotation(MappingTable.class);
            if(mappTabl != null)
            {
                result.add(fields[i]);
            }
        }

        return result;
    }

    /**
     * 获取关联表信息
     * @param data
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public static List<Object> getMappTabl(List<? extends AutoImportInterface> data) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        List<Object> result = new ArrayList();

        MappingTable mappTabl = null;
        Field field = null;
        String fieldName = null;
        Method m = null;
        if(data.size() >0)
        {
            Class cla=data.get(0).getClass();
            List<Field> fieldList= getMappingField(cla);
            if(fieldList.size() > 0)
            {
                for(int i=0,end =data.size();i < end;i++)
                {
                    for(int i2=0,end2=fieldList.size();i2 < end2 ;i2++)
                    {
                        field = fieldList.get(i2);
                        fieldName = field.getName();

                        //判断是否能获取字段
                        m = cla.getMethod("get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1));
                        if( m !=  null)
                        {
                            String val= (String) m.invoke(data.get(i));

                            //只有当值不为空的时候，才添加
                            if(val != null)
                            {
                                mappTabl=field.getAnnotation(MappingTable.class);
                                Object obj=mappTabl.clas().newInstance();

                                //写入字段信息
                                m = mappTabl.clas().getMethod("set"+mappTabl.autoFiled().substring(0,1).toUpperCase()+mappTabl.autoFiled().substring(1),String.class);
                                if(m != null)
                                {
                                    m.invoke(obj,val);
                                }

                                //写入外键信息
                                m = mappTabl.clas().getMethod("set"+mappTabl.fkey().substring(0,1).toUpperCase()+mappTabl.fkey().substring(1),String.class);
                                if(m != null)
                                {
                                    m.invoke(obj,data.get(i).getId());
                                }
                                result.add(obj);
                            }



                        }
                    }
                }
            }


        }
        return result;
    }

    /**
     * 自动注入 id，当前时间，当前登录人
     * @param data
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void autoFiled(List data) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {



        if(data.size() > 0 )
        {
            Map<String,Method> keyMethod = getKeyMethod(data.get(0).getClass());

            for( int i = 0,end = data.size() ;i < end ;i++ )
            {
                //注入ID
                Method m= keyMethod.get("autoId");
                if(m != null)
                {
                    m.invoke(data.get(i), StringUtils.getRandUUID());
                }

                /*
                //注入当前时间
                m= keyMethod.get("autoDate");
                if(m != null)
                {
                    m.invoke(data.get(i), StringUtils.getCurrentDateTimeDefault());
                }


                //注入当前登录人姓名
                m= keyMethod.get("autoLoginUser");
                if(m != null)
                {
                    UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
                    m.invoke(data.get(i), sessionVO.getUsername());
                }
                */
                //注入外检任务
                m= keyMethod.get("autoExinspTask");
                if(m != null)
                {
                    m.invoke(data.get(i), getExInspTaskId());
                }

                BeanUtils.setProperty(data.get(i),"dataStated","20");
                BeanUtils.setProperty(data.get(i),"isNewset","0");
            }
        }


    }

    /**
     * 获取外检任务
     * @return
     */
    public static String getExInspTaskId()
    {
        return  RequestResponseContext.getRequest().getParameter("exInspTaskId");
    }
    /**
     * 获取外检任务
     * @return
     */
    public static String getBaseId()
    {
        return  RequestResponseContext.getRequest().getParameter("baseid");
    }



    public static String  getClassMappingTable(Class cla)
    {
        DbTable table = (DbTable) cla.getAnnotation(DbTable.class);
        if(table != null)
        {
            return table.name();
        }
        return null;
    }

    /**
     * 根据类，获取插入sql
     * @param cla
     * @return
     * @throws NoSuchMethodException
     */
    public static String getClassToSql(Class cla) throws NoSuchMethodException, IllegalAccessException, InstantiationException {
        StringBuilder result=new StringBuilder("insert into  ");//insert into
        DbFiled dbFiled = null;

        DbTable table = (DbTable) cla.getAnnotation(DbTable.class);
        if(table != null)
        {
            result.append(table.name()).append(" (");
            Field fields[] = cla.getDeclaredFields();
            for(int fi=0,fend=fields.length;fi < fend;fi++)
            {
                dbFiled = fields[fi].getAnnotation(DbFiled.class);
                if(dbFiled != null)
                {
                    result.append(dbFiled.name()).append(" ,");
                }else{
                    System.out.println(fields[fi].getName());
                }

            }
            if(fields.length > 0)
            {
                result.deleteCharAt(result.length()-1);
            }
//            result.append("from ");
//            result.append(table.name());
            result.append(",CREATE_DATE,CREATE_USER)").append(" values ");
        }
        return result.toString();
    }


    /**
     * 获取对象值
     * @param obj
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static String geObjectFiledValue(Object obj) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ParseException {
        Class cla=obj.getClass();

        StringBuilder result=new StringBuilder("(");//insert into
        DbFiled dbFiled = null;

        DbTable table = (DbTable) cla.getAnnotation(DbTable.class);
        String filedName=null;
        DbFiled filed=null;
        if(table != null)
        {
            Field fields[] = cla.getDeclaredFields();
            for(int fi=0,fend=fields.length;fi < fend;fi++)
            {
                dbFiled = fields[fi].getAnnotation(DbFiled.class);
                if(dbFiled != null)
                {
                    filed=fields[fi].getAnnotation(DbFiled.class);
                    filedName = fields[fi].getName();
                    Method getm=cla.getMethod("get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1));
                    if(getm != null )
                    {
                        String val= (String) getm.invoke(obj);
                        if(StringUtils.isEmpty(val))
                        {
                            result.append("null").append(",");
                        }else{
                            if("NUMBER".equals(filed.type()))
                            {
                                result.append(getm.invoke(obj)).append(" ,");
                            }else if("DATE".equals(filed.type()))
                            {
                                if(StringUtils.isEmpty(filed.formate()))
                                {
                                    result.append("to_date('").append(StringUtils.convertDateToString(getDate(val),StringUtils.DATE_TIME_DEFAULT)).append("','yyyy-mm-dd hh24:mi:ss') ,");

                                }else{
                                    result.append("to_date('").append(StringUtils.convertDateToString(getDate(val,filed.formate()),StringUtils.DATE_TIME_DEFAULT)).append("','yyyy-mm-dd hh24:mi:ss') ,");

                                }

                            }else{
                                result.append("'").append(getm.invoke(obj)).append("' ,");
                            }
                        }


                    }

                }else{
                    System.out.println(fields[fi].getName());
                }

            }
            if(fields.length > 0)
            {
                result.deleteCharAt(result.length()-1);
            }
            UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
            result.append(",sysdate,'").append(sessionVO.getId()).append("'");
            result.append(")");

        }
        return result.toString();
    }


    private static Map<String,Method> getKeyMethod(Class cla) throws NoSuchMethodException {
        AutoId autoId = null;
        AutoDate autoDate = null;
        AutoLoginUser autoLoginUser = null;
        AutoExinspTask autoExinspTask = null;
        Map<String,Method> keyMethod = new HashMap<String,Method>();
        Field fields[] = cla.getDeclaredFields();

        for(int fi=0,fend=fields.length;fi < fend;fi++)
        {
            autoId = fields[fi].getAnnotation(AutoId.class);
            if(autoId != null)
            {

                Method m= cla.getMethod("set"+getMethodName(fields[fi]),String.class);
                keyMethod.put("autoId",m);
            }
            autoDate = fields[fi].getAnnotation(AutoDate.class);
            if(autoDate != null)
            {

                Method m= cla.getMethod("set"+getMethodName(fields[fi]),String.class);
                keyMethod.put("autoDate",m);
            }

            autoLoginUser = fields[fi].getAnnotation(AutoLoginUser.class);
            if(autoLoginUser != null)
            {

                Method m= cla.getMethod("set"+getMethodName(fields[fi]),String.class);
                keyMethod.put("autoLoginUser",m);
            }
            autoExinspTask = fields[fi].getAnnotation(AutoExinspTask.class);
            if(autoExinspTask != null)
            {

                Method m= cla.getMethod("set"+getMethodName(fields[fi]),String.class);
                keyMethod.put("autoExinspTask",m);
            }

        }

        return keyMethod;
    }

    private static String getMethodName(Field f)
    {
        String name = f.getName();

        return name.substring(0,1).toUpperCase()+name.substring(1);
    }

    public static  String [] getFiledNames(Class c)
    {
        List<String> data=new ArrayList<String>();
        Field fields[] = c.getDeclaredFields();
        Head head=null;
        for(int fi=0,fend=fields.length;fi < fend;fi++)
        {
            head = fields[fi].getAnnotation(Head.class);
            if(head != null)
            {
                data.add(head.name());
            }
        }

        int size=data.size();
        String resullt[]=new String[size];
        for(int i=0,end=size;i < end;i++)
        {
            resullt[i]=data.get(i);
        }
        return resullt;
    }
    private static Date getDate(String val,String formate) throws ParseException {

        return  new SimpleDateFormat(formate).parse(val);
    }

    private static Date getDate(String val) throws ParseException {
        String dateFormate[]={"yyyyMMdd","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd","yyyyMM"};
        if(val.indexOf("-") == -1)
        {
            if(val.length() == 6)
            {
                return  new SimpleDateFormat(dateFormate[3]).parse(val);
            }else{
                return  new SimpleDateFormat(dateFormate[0]).parse(val);
            }

        }else if(val.indexOf("-") == 4 &&val.length() == dateFormate[1].length()){
            return  new SimpleDateFormat(dateFormate[1]).parse(val);
        }else{
            return  new SimpleDateFormat(dateFormate[2]).parse(val);
        }

    }
}
